define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index',
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'order_id',
                sortName: 'order_id',
                columns: [
                    [
//                        {checkbox: true},
                        {field: 'order_id', title: __('Order_id')},
                        {field: 'order_no', title: __('Order_no')},
//                        {field: 'user_id', title: __('User_id')},
//                        {field: 'task_id', title: __('Task_id')},
                        {field: 'task_title', title: __('Task_title')},
                        {field: 'phone', title: __('Phone')},
                        {field: 'position_name', title: __('Position_name')},
                        {field: 'order_status', title: __('Order_status'), visible: false, searchList: {"0": __('Order_status 0'), "1": __('Order_status 1'), "2": __('Order_status 2'), "10": __('Order_status 10'), "11": __('Order_status 11'), "12": __('Order_status 12'), "13": __('Order_status 13'), "20": __('Order_status 20'), "90": __('Order_status 90'), "91": __('Order_status 91'), "92": __('Order_status 92')}},
                        {field: 'order_status_text', title: __('Order_status'), operate: false},
                        {field: 'task_price', title: __('Task_price'),operate: false},
                        {field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
//                        {field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
//                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
                        {
                            field: 'order_id',
                            title: __('操作'),
                            operate: false,
                            width: '100px',
                            formatter: Controller.api.formatter.opt
                        },
                    ]
                ]
            });
             //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#','');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    if(typeStr==1){
                        params.filter = JSON.stringify({order_status: 0});
                    }
                    
                    if(typeStr==2){
                        params.filter = JSON.stringify({order_status: [1,2,10,11,12]});
                        params.op = JSON.stringify({order_status: 'IN'});
                    }
                    
                    if(typeStr==3){
                        params.filter = JSON.stringify({order_status: 20});
                    }
                    
                    if(typeStr==4){
                        params.filter = JSON.stringify({order_status: [90,91,92]});
                        params.op = JSON.stringify({order_status: 'IN'});
                    }
//                    params.filter = JSON.stringify({order_status: typeStr});
//                    params.type = typeStr;
//                    params.status = typeStr;
                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {//渲染的方法

                opt: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    
                    var html = '<a href="order/detail/ids/'+value+'" class="btn-change text-success" title="任务订单详情"><b>任务订单详情</b></a><br/>';
              
                    return html;
                }
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});